package controller;

import Service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import entity.Users;
import org.apache.struts2.ServletActionContext;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

public class UserRegisterAction extends ActionSupport  {
     private String userName;
     private String userPassword1;
     private String userPassword2;
     private String userTelephone;
     private String userName2;
     private String errStr;
        @Override
        public String execute() throws Exception {
            UserService userService = new UserService();
            System.out.println(userPassword1);
            if (userPassword1.equals(userPassword2)){
                Users users = new Users(userName,userPassword1,userTelephone,userName2,1);
                boolean b = userService.regi(users);
                if (b){
                  Map<String, Object> sessionMap = ActionContext.getContext().getSession();
                  sessionMap.put("sesUser",users);
                  errStr="注册成功了";
                    ServletActionContext.getRequest().setAttribute("errStr3",errStr);
                  return "regSuc";
                }else{
                    errStr="注册失败了";
                    ServletActionContext.getRequest().setAttribute("errStr1",errStr);
                    return "error";
                 }
            }else {
                errStr="前后密码不一致";
                ServletActionContext.getRequest().setAttribute("errStr2",errStr);
                return "error";
            }

        }

    public String getErrStr() {
        return errStr;
    }

    public void setErrStr(String errStr) {
        this.errStr = errStr;
    }

    public String getUsername() {
        return userName;
    }

    public void setUsername(String username) {
        this.userName = username;
    }

    public String getUserPassword1() {
        return userPassword1;
    }

    public void setUserPassword1(String userPassword1) {
        this.userPassword1 = userPassword1;
    }

    public String getUserPassword2() {
        return userPassword2;
    }

    public void setUserPassword2(String userPassword2) {
        this.userPassword2 = userPassword2;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserTelephone() {
        return userTelephone;
    }

    public void setUserTelephone(String userTelephone) {
        this.userTelephone = userTelephone;
    }

    public String getUserName2() {
        return userName2;
    }

    public void setUserName2(String userName2) {
        this.userName2 = userName2;
    }
}
